<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：文章控制器。
 *
 **/

namespace Qwadmin\Controller;


class ParameterController extends ComController{

    public function add(){
        $Foodstuff = M("foodstuff");
        $foodstuff_array = $Foodstuff->order("o asc")->select();
        $this->assign("foodstuff_array",$foodstuff_array);
        $this->display("form");
    }


    public function index($sid = 0, $p = 1){
        $Parameter = M("Parameter");
        $pagesize = 20;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $prefix = C('DB_PREFIX');
        $where = "1=1";
        $count = $Parameter->where($where)->count();
        $list = $Parameter
            ->where($where)
//            ->order("o asc")
            ->limit($offset . ',' . $pagesize)->select();

        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }


    public function del(){
        $id = I("get.id");
        if ($id) {
            $map['id'] = $id;
            if (M('Parameter')->where($map)->delete()) {
                $map_set["parameter_id"] = $id;
                M("Parameter_setting")->where($map_set)->delete();
                addlog('删除参数，ID：' . $id);
                $this->success('参数删除成功！');
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }
    }

    public function edit(){
        $id = I("get.id");
        $res = M('Parameter')->find($id);
        $where['parameter_id'] = $id;
        $setting_res = M("parameter_setting")->where($where)->find();

        $shuifen_array = unserialize($setting_res['shuifen']);
        $shengmei_array = unserialize($setting_res['shengmei']);
        $zazhi_array = unserialize($setting_res['zazhi']);
        $chucaolv_array = unserialize($setting_res['zazhi']);
        $guwaicaomilv_array = unserialize($setting_res['guwaicaomilv']);
        $zhengjingmilv_array = unserialize($setting_res['zhengjingmilv']);
        $huanglimi_array = unserialize($setting_res['huanglimi']);

//        dump($shuifen_array);exit;
        $this->assign("shuifen_array",$shuifen_array);
        $this->assign("shengmei_array",$shengmei_array);
        $this->assign("zazhi_array",$zazhi_array);
        $this->assign("chucaolv_array",$chucaolv_array);
        $this->assign("guwaicaomilv_array",$guwaicaomilv_array);
        $this->assign("zhengjingmilv_array",$zhengjingmilv_array);
        $this->assign("huanglimi_array",$huanglimi_array);

        $Foodstuff = M("foodstuff");
        $foodstuff_array = $Foodstuff->order("o asc")->select();
        $this->assign("foodstuff_array",$foodstuff_array);
        $this->assign("res",$res);
        $this->display('form');
    }

    public function update(){
        $Parameter = D("parameter");
        if(!$Parameter->create()){
            $this->error($Parameter->getError());
        }
        $data = I("post.");
        if(isset($data['id']) && $data['id']!=""){
            $res = $Parameter->data($data)->save();
            if($res!==false){
                $Parameter_setting = M("parameter_setting");
                $Parameter_setting->where("parameter_id=".$data['id'])->delete();
                $setting_data['parameter_id'] = $data['id'];

                $setting_data['shuifen'] = serialize(array("number"=>I("shuifen_number"),'plus'=>I("shuifen_plus"),'minus'=>I("shuifen_minus"),'shailiang'=>I('shuifen_shailiang_minus')));
                $setting_data['zazhi'] = serialize(array("number"=>I("zazhi_number"),'plus'=>I("zazhi_plus"),'minus'=>I("zazhi_minus"),'qingzafei'=>I('zazhi_qingzafei_minus')));
                $setting_data['shengmei'] = serialize(array("number"=>I("shengmei_number"),'plus'=>I("shengmei_plus"),'minus'=>I("shengmei_minus")));
                $setting_data['chucaolv'] = serialize(array("number"=>I("chucaolv_number"),'plus'=>I("chucaolv_plus"),'minus'=>I("chucaolv_minus")));
                $setting_data['zhengjingmilv'] = serialize(array("number"=>I("zhengjingmilv_number"),'plus'=>I("zhengjingmilv_plus"),'minus'=>I("zhengjingmilv_minus")));
                $setting_data['guwaicaomilv'] = serialize(array("number"=>I("guwaicaomilv_number"),'plus'=>I("guwaicaomilv_plus"),'minus'=>I("guwaicaomilv_minus")));
                $setting_data['huanglimi'] = serialize(array("number"=>I("huanglimi_number"),'plus'=>I("huanglimi_plus"),'minus'=>I("huanglimi_minus")));

                $setting_id = $Parameter_setting->data($setting_data)->add();

                if($setting_id){
                    $this->success("参数编辑成功!",U("index"));
                }else{
                    $this->error("参数编辑失败!");
                }

            }else{
                $this->success("参数编辑失败!");
            }


        }else{
            $check = checkParameter($data['foodstuff_id'],$data['level']);
            if($check==false){
                $this->error(getFoodstuffNameById($data['foodstuff_id']).$data['level']."级的参数已经设定");
            }
            //新增凭证
            $parameter_id = $Parameter->data($data)->add();
            if($parameter_id){
                $Parameter_setting = M("parameter_setting");
                $setting_data['parameter_id'] = $parameter_id;
                $setting_data['shuifen'] = serialize(array("number"=>I("shuifen_number"),'plus'=>I("shuifen_plus"),'minus'=>I("shuifen_minus"),'shailiang'=>I('shuifen_shailiang_minus')));
                $setting_data['zazhi'] = serialize(array("number"=>I("zazhi_number"),'plus'=>I("zazhi_plus"),'minus'=>I("zazhi_minus"),'qingzafei'=>I('zazhi_qingzafei_minus')));
                $setting_data['shengmei'] = serialize(array("number"=>I("shengmei_number"),'plus'=>I("shengmei_plus"),'minus'=>I("shengmei_minus")));
                $setting_data['chucaolv'] = serialize(array("number"=>I("chucaolv_number"),'plus'=>I("chucaolv_plus"),'minus'=>I("chucaolv_minus")));
                $setting_data['zhengjingmilv'] = serialize(array("number"=>I("zhengjingmilv_number"),'plus'=>I("zhengjingmilv_plus"),'minus'=>I("zhengjingmilv_minus")));
                $setting_data['guwaicaomilv'] = serialize(array("number"=>I("guwaicaomilv_number"),'plus'=>I("guwaicaomilv_plus"),'minus'=>I("guwaicaomilv_minus")));
                $setting_data['huanglimi'] = serialize(array("number"=>I("huanglimi_number"),'plus'=>I("huanglimi_plus"),'minus'=>I("huanglimi_minus")));

                $setting_id = $Parameter_setting->data($setting_data)->add();
                if($setting_id){
                    $this->success("参数添加成功!",U("index"));
                }else{
                    $this->error("参数添加失败!");
                }

            }else{
                $this->error("参数添加失败!");
            }
        }
    }


}